import { isUrl } from '@ant-design/pro-utils';
import lodash from 'lodash';
import { parse, stringify } from 'qs';
import { SfwRoute } from './sfwTypes';

export default function convertToUmiRoutes(
  routes: SfwRoute[],
  addAppName?: boolean,
) {
  const routesTemp = lodash.cloneDeep(routes);
  lodash.forEach(routesTemp, (businessPage: any) => {
    if (businessPage.app?.isMaster) {
      return;
    }
    // 如果是 url 不进行应用名称拼接
    if (isUrl(businessPage.path)) {
      return;
    }
    if (addAppName) {
      businessPage.path = `/${businessPage.app?.name || ''}${
        businessPage.path
      }`;
    }
  });
  return routesTemp;
}

export function getPathUrl(route: Record<string, any>) {
  if (!route) {
    return '/';
  }
  const urlSearch = parse(route.params?.split('?')[1]);
  const urlSearchOmit = stringify(lodash.omit(urlSearch, 'props'));
  return `${route.path}${
    lodash.isEmpty(urlSearchOmit) ? '' : `?${urlSearchOmit}`
  }`;
}
